package com.sync;

/**
 * 对象锁 方法锁形式
 * */
public class SyncObjectMethod2 implements Runnable {
    static SyncObjectMethod2 sync = new SyncObjectMethod2();
    static int i = 0;
    @Override
    public void run() {
        method();
    }

    public synchronized void method(){
        System.out.println("我是对象所的方法修饰符形式,我叫"+Thread.currentThread().getName());
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {

        }
        System.out.println("名称叫"+Thread.currentThread().getName()+ " 运行结束");
    }


    public static void main(String[] args) {
        Thread t1 = new Thread(sync);
        Thread t2 = new Thread(sync);
        t1.start();
        t2.start();
        //通过isAlive方法检测是否线程还在继续存活
        while(t1.isAlive() || t2.isAlive());

        System.out.println("finished");

    }
}
